//
//  TestSplitViewController.swift
//  DemoProject
//
//  Created by Hao on 2018/8/31.
//  Copyright © 2018年 hao. All rights reserved.
//

/// UIPageViewController使用 https://www.jianshu.com/p/46c8c585d50b

import UIKit

// MARK: 测试的控制器
fileprivate class TestPageDemo01VC: BaseViewController {
    
    var contentStr: String? {
        didSet {
            contentLabel.text = contentStr
        }
    }
    
    private lazy var contentLabel: UILabel = {
        let _label = UILabel()
        self.view.addSubview(_label)
        _label.textAlignment = .center
        _label.textColor = kRandomColor()
        _label.numberOfLines = 0
        _label.snp.makeConstraints({ (make) in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.centerY.equalTo(self.view)
        })
        return _label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = kRandomColor()
    }
    
}

class TestPageViewController: BaseViewController {

    private lazy var pageContentArray: [String] = {
        var _array = [String]()
        for i in 0 ..< 10 {
            _array.append("This is the page \(i) of content displayed using UIPageViewController")
        }
        return _array
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // pageCurl: 页面卷曲(卷页)   scroll: 平滑的滚动
        // UIPageViewControllerOptionInterPageSpacingKey: 翻页时的间距, 默认 0
        // 如果 options 设置了 UIPageViewControllerSpineLocationMid,注意viewControllers至少包含两个数据,且 doubleSided = YES
//        let options = [UIPageViewControllerOptionSpineLocationKey : UIPageViewControllerSpineLocation.mid.rawValue] as [String : Any]
        let pageController = UIPageViewController(transitionStyle: .pageCurl, navigationOrientation: .horizontal, options: nil)
        pageController.delegate = self
        pageController.dataSource = self
        
        // 让UIPageViewController对象，显示相应的页数据。
        // UIPageViewController对象要显示的页数据封装成为一个NSArray。
        // 因为我们定义UIPageViewController对象显示样式为显示一页（options参数指定）。
        // 如果要显示2页，NSArray中，应该有2个相应页数据。
        
        // 设置UIPageViewController初始化数据, 将数据放在NSArray里面
        // 如果 options 设置了 UIPageViewControllerSpineLocationMid,注意viewControllers至少包含两个数据,且 doubleSided = YES
        if let controller = _viewControllerAtIndex(0) {
            pageController.setViewControllers([controller], direction: .reverse, animated: false, completion: nil)
            pageController.view.frame = self.view.bounds
            addChildViewController(pageController)
            self.view.addSubview(pageController.view)
        }
    }
    
    // 根据index得到对应的Controller
    fileprivate func _viewControllerAtIndex(_ index: Int) -> TestPageDemo01VC? {
        if index >= pageContentArray.count || index < 0 {
            return nil
        }
        let vc = TestPageDemo01VC()
        vc.contentStr = pageContentArray[index]
        return vc
    }
    
    // 根据控制器内容获取下标
    fileprivate func _indexOfViewController(_ controller: TestPageDemo01VC) -> Int? {
        return pageContentArray.index(of: controller.contentStr ?? "")
    }

}

// MARK: UIPageViewControllerDelegate
extension TestPageViewController: UIPageViewControllerDelegate {
    
    
    
}

// MARK: UIPageViewControllerDataSource
extension TestPageViewController: UIPageViewControllerDataSource {
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        print(info: "")
        guard var index = _indexOfViewController(viewController as! TestPageDemo01VC) else { return nil }
        if (index == 0) || (index == NSNotFound) {
            return nil
        }
        index -= 1
        // 返回的ViewController，将被添加到相应的UIPageViewController对象上。
        // UIPageViewController对象会根据UIPageViewControllerDataSource协议方法,自动来维护次序
        // 不用我们去操心每个ViewController的顺序问题
        return _viewControllerAtIndex(index)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        print(info: "")
        guard var index = _indexOfViewController(viewController as! TestPageDemo01VC) else { return nil }
        if index == NSNotFound {
            return nil
        }
        index += 1
        if index == pageContentArray.count {
            return nil
        }
        return _viewControllerAtIndex(index)
    }
    
}

